﻿Imports IWshRuntimeLibrary
Imports System.IO

Public Class MainForm

    Private Sub ShowMessage(ByVal strContent As String)
        MessageBox.Show(strContent, "VSS切换")
    End Sub

    ''' <summary>
    ''' 模式 0 设置模式 1 为快捷模式
    ''' </summary>
    ''' <remarks></remarks>
    Private _mode As Integer = 0
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim args() As String = Environment.GetCommandLineArgs()
        If args.Length = 1 Then
            LoadData()
            _mode = 0
            GoTo 正常模式
        End If
        If Not IsNumeric(args(1)) Then
            ShowMessage("参数错误")
            Me.Close()
            Exit Sub
        End If
        _mode = CInt(args(1))
        LoadData()
        Dim strRow As Integer
正常模式:
        If _mode <> 0 Then
            If args.Length < 3 Then
                ShowMessage("参数错误")
                Me.Close()
                Exit Sub
            Else
                strRow = CInt(args(2))
                If strRow > Me.DG.Rows.Count - 1 Then
                    ShowMessage("参数错误")
                    Me.Close()
                    Exit Sub
                End If
            End If
            'SSDIR
            'SSUSER
            'SSPWD
            'pat
            Me.Hide()
            Dim strUserName As String = Me.DG.Rows(strRow).Cells(0).Value.ToString
            Dim strPassword As String = Me.DG.Rows(strRow).Cells(1).Value.ToString
            Dim strDir As String = System.IO.Path.GetDirectoryName(Me.DG.Rows(strRow).Cells(3).Value.ToString)
            Dim strExe As String = Me.DG.Rows(strRow).Cells(2).Value.ToString
            Dim strLastDir As String = Me.DG.Rows(strRow).Cells(4).Value.ToString
            Dim strEncoding As String = Me.DG.Rows(strRow).Cells(5).Value.ToString

            Environment.SetEnvironmentVariable("SSUSER", strUserName)
            Environment.SetEnvironmentVariable("SSPWD", strPassword)
            Environment.SetEnvironmentVariable("SSDIR", strDir)

            If System.IO.File.Exists(Me.DG.Rows(strRow).Cells(2).Value.ToString) Then
                Try
                    Dim dir1 As String = ""
                    dir1 = Path.Combine(strDir, "users\" & strUserName & "\ss.ini")
                    Dim lstContent As New List(Of String)

                    '获取文件编码
                    Dim en As System.Text.Encoding
                    Try
                        en = System.Text.Encoding.GetEncoding(CInt(strEncoding))
                        If en Is Nothing Then
                            en = System.Text.Encoding.Default
                        End If
                    Catch ex As Exception
                        en = System.Text.Encoding.Default
                    End Try

                    '修改INI最后一次目录
                    lstContent.AddRange(System.IO.File.ReadAllLines(dir1, en))
                    If lstContent.Count > 0 Then
                        For i As Integer = 0 To lstContent.Count - 1
                            If lstContent(i).StartsWith("Project = ") Then
                                lstContent(i) = "Project = " & strLastDir
                                Exit For
                            End If
                        Next
                        System.IO.File.WriteAllLines(dir1, lstContent.ToArray, en)
                    End If
                Catch ex As Exception

                End Try
            Else
                ShowMessage(Me.DG.Rows(strRow).Cells(2).Value.ToString & " 不存在 ")
                Me.Close()
                Exit Sub
            End If

            If System.IO.File.Exists(strExe) Then
                System.Diagnostics.Process.Start(strExe)
            Else
                ShowMessage(strExe & " 不存在 ")
                Me.Close()
                Exit Sub
            End If

            Me.Close()
        Else
            Dim vss As New CFormSetting
            Dim dir As String = Me.GetType().Assembly.Location
            dir = System.IO.Path.GetDirectoryName(dir)
            Dim strFile As String = System.IO.Path.Combine(dir, "CFormSetting.DB")
            vss.FileToDeSerializable(strFile)
            If vss.fs1 IsNot Nothing Then
                Me.Width = vss.fs1.width
                Me.Height = vss.fs1.height
            End If
        End If
    End Sub

    Private Sub LoadData()
        Me.VSSShiftDB.VSSSHIFTDT.Clear()
        Dim vss As New CVSS
        Dim dir As String = Me.GetType().Assembly.Location
        dir = System.IO.Path.GetDirectoryName(dir)
        Dim strFile As String = System.IO.Path.Combine(dir, "VSSSHIFT.DB")
        vss.FileToDeSerializable(strFile)

        For i As Integer = 0 To vss.lstVss.Count - 1
            Me.VSSShiftDB.VSSSHIFTDT.Rows.Add(New String() {vss.lstVss(i).USERNAME, _
                                                            vss.lstVss(i).PASSWORD, _
                                                            vss.lstVss(i).VSSSETUPPATH, _
                                                            vss.lstVss(i).VSSDIR, _
                                                            vss.lstVss(i).LastDir, _
                                                            vss.lstVss(i).EncodePage})
        Next

        For I As Integer = 0 To Me.DG.Rows.Count - 1
            DG.Rows(I).HeaderCell.Value = (I + 1).ToString
            DG.Rows(I).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        Next

    End Sub

    Private Sub VSSSHIFTDTBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VSSSHIFTDTBindingNavigatorSaveItem.Click
        Dim vss As New CVSS
        For idex As Integer = 0 To DG.Rows.Count - 1
            vss.AddVss(DG.Rows(idex).Cells(0).Value.ToString, _
                       DG.Rows(idex).Cells(1).Value.ToString, _
                       DG.Rows(idex).Cells(2).Value.ToString, _
                       DG.Rows(idex).Cells(3).Value.ToString, _
                       DG.Rows(idex).Cells(4).Value.ToString)
        Next
        Dim dir As String = Me.GetType().Assembly.Location
        dir = System.IO.Path.GetDirectoryName(dir)
        Dim strFile As String = System.IO.Path.Combine(dir, "VSSSHIFT.DB")
        vss.SerializableToFile(strFile)
        LoadData()
        ShowMessage(" 保存成功 ")
    End Sub

    Public Function CreatLnkFile(ByVal lnkFile As String, ByVal ExeFilePath As String, ByVal iDescription As String, ByVal strArg As String) As Boolean
        Try
            Dim iconNumber As Integer = 0
            Dim CreatDir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            Dim wShell As New IWshShell_Class
            Dim shortCut As IWshRuntimeLibrary.IWshShortcut
            shortCut = CType(wShell.CreateShortcut(CreatDir & "\" & lnkFile & ".lnk"), IWshShortcut)
            shortCut.TargetPath = ExeFilePath & " "
            shortCut.WindowStyle = 1
            shortCut.Arguments = strArg
            shortCut.Description = iDescription
            shortCut.WorkingDirectory = ""
            shortCut.IconLocation = ExeFilePath & ", " & iconNumber
            shortCut.Save()
            wShell = Nothing
            ShowMessage("创建成功")
            Return True
        Catch ex As System.Exception
            ShowMessage("创建失败 " & ex.Message)
        End Try
        Return False
    End Function

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Using frm As New VSSForm
            If Me.DG.Rows.Count > 0 Then
                Dim index As Integer = Me.DG.SelectedRows(0).Index
                Dim vs As New VSS
                vs.USERNAME = DG.Rows(index).Cells(0).Value.ToString
                vs.PASSWORD = DG.Rows(index).Cells(1).Value.ToString
                vs.VSSSETUPPATH = DG.Rows(index).Cells(2).Value.ToString
                vs.VSSDIR = DG.Rows(index).Cells(3).Value.ToString
                vs.LastDir = DG.Rows(index).Cells(4).Value.ToString
                vs.EncodePage = DG.Rows(index).Cells(5).Value.ToString
                frm.SetVss(vs)
            End If
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim vss As VSS = frm.GetVss
                Me.VSSShiftDB.VSSSHIFTDT.Rows.Add(New String() {vss.USERNAME, _
                                                                vss.PASSWORD, _
                                                                vss.VSSDIR, _
                                                                vss.VSSSETUPPATH, _
                                                                vss.LastDir, vss.EncodePage})

            End If
        End Using
    End Sub

    Private Sub DG_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG.CellDoubleClick
        If e.RowIndex < 0 Then
            Exit Sub
        End If

        Dim frm As New VSSForm
        frm.Text = "修改窗体"
        Dim vs As New VSS
        vs.USERNAME = DG.Rows(e.RowIndex).Cells(0).Value.ToString
        vs.PASSWORD = DG.Rows(e.RowIndex).Cells(1).Value.ToString
        vs.VSSSETUPPATH = DG.Rows(e.RowIndex).Cells(2).Value.ToString
        vs.VSSDIR = DG.Rows(e.RowIndex).Cells(3).Value.ToString
        vs.LastDir = DG.Rows(e.RowIndex).Cells(4).Value.ToString
        vs.EncodePage = DG.Rows(e.RowIndex).Cells(5).Value.ToString
        frm.SetVss(vs)
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            vs = frm.GetVss
            DG.Rows(e.RowIndex).Cells(0).Value = vs.USERNAME
            DG.Rows(e.RowIndex).Cells(1).Value = vs.PASSWORD
            DG.Rows(e.RowIndex).Cells(2).Value = vs.VSSSETUPPATH
            DG.Rows(e.RowIndex).Cells(3).Value = vs.VSSDIR
            DG.Rows(e.RowIndex).Cells(4).Value = vs.LastDir
            DG.Rows(e.RowIndex).Cells(5).Value = vs.EncodePage
        End If
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        If Me.DG.Rows.Count = 0 Then
            Exit Sub
        End If

        If Me.DG.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        CreatLnkFile("VSSShift" & Me.DG.SelectedRows(0).Index, _
                     Me.GetType().Assembly.Location, _
                     "", _
                     " 1 " & Me.DG.SelectedRows(0).Index)
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Escape Then
            Me.Close()
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub DG_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DG.CellFormatting
        If e.ColumnIndex = 1 Then
            If e.Value IsNot Nothing AndAlso e.Value.ToString.Length > 0 Then
                e.Value = New String("*", e.Value.ToString.Length)
            End If
        End If
    End Sub

    Private Sub DG_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DG.EditingControlShowing
        Dim t As TextBox = e.Control
        If t IsNot Nothing Then
            If Me.DG.CurrentCell.ColumnIndex = 1 Then
                t.PasswordChar = "*"
            Else
                t.PasswordChar = ""
            End If
        End If
    End Sub

    Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If _mode = 0 Then
            Dim vss As New CFormSetting
            vss.fs1.width = Me.Width
            vss.fs1.height = Me.Height
            Dim dir As String = Me.GetType().Assembly.Location
            dir = System.IO.Path.GetDirectoryName(dir)
            Dim strFile As String = System.IO.Path.Combine(dir, "CFormSetting.DB")
            vss.SerializableToFile(strFile)
        End If
    End Sub

    Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub 关于ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于ToolStripMenuItem.Click
        Using frm As New AboutBox1
            frm.ShowDialog(Me)
        End Using
    End Sub

    Private Sub 快捷方式ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 快捷方式ToolStripMenuItem.Click
        ToolStripButton1_Click(sender, e)
    End Sub

    Private Sub 保存ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存ToolStripMenuItem.Click
        VSSSHIFTDTBindingNavigatorSaveItem_Click(sender, e)
    End Sub

    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        If Me.DG.RowCount = 0 Then
            Exit Sub
        End If
        If Me.DG.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        VSSSHIFTDTBindingSource.RemoveCurrent()
    End Sub

    Private Sub 新建ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 新建ToolStripMenuItem.Click

        BindingNavigatorAddNewItem_Click(sender, e)

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        If Me.DG.SelectedRows.Count = 0 Then Exit Sub
        Dim strRow As Integer = Me.DG.SelectedRows(0).Index

        Dim strUserName As String = Me.DG.Rows(strRow).Cells(0).Value.ToString
        Dim strPassword As String = Me.DG.Rows(strRow).Cells(1).Value.ToString
        Dim strDir As String = System.IO.Path.GetDirectoryName(Me.DG.Rows(strRow).Cells(3).Value.ToString)
        Dim strExe As String = Me.DG.Rows(strRow).Cells(2).Value.ToString
        Dim strLastDir As String = Me.DG.Rows(strRow).Cells(4).Value.ToString
        Dim strEncoding As String = Me.DG.Rows(strRow).Cells(5).Value.ToString

        Environment.SetEnvironmentVariable("SSUSER", strUserName)
        Environment.SetEnvironmentVariable("SSPWD", strPassword)
        Environment.SetEnvironmentVariable("SSDIR", strDir)

        If System.IO.File.Exists(Me.DG.Rows(strRow).Cells(2).Value.ToString) Then
            Try
                Dim dir1 As String = ""
                dir1 = Path.Combine(strDir, "users\" & strUserName & "\ss.ini")
                Dim lstContent As New List(Of String)

                '获取文件编码
                Dim en As System.Text.Encoding
                Try
                    en = System.Text.Encoding.GetEncoding(CInt(strEncoding))
                    If en Is Nothing Then
                        en = System.Text.Encoding.Default
                    End If
                Catch ex As Exception
                    en = System.Text.Encoding.Default
                End Try

                '修改INI最后一次目录
                lstContent.AddRange(System.IO.File.ReadAllLines(dir1, en))
                If lstContent.Count > 0 Then
                    For i As Integer = 0 To lstContent.Count - 1
                        If lstContent(i).StartsWith("Project = ") Then
                            lstContent(i) = "Project = " & strLastDir
                            Exit For
                        End If
                    Next
                    System.IO.File.WriteAllLines(dir1, lstContent.ToArray, en)
                End If
            Catch ex As Exception

            End Try
        Else
            ShowMessage(Me.DG.Rows(strRow).Cells(2).Value.ToString & " 不存在 ")
            Me.Close()
            Exit Sub
        End If

        If System.IO.File.Exists(strExe) Then
            System.Diagnostics.Process.Start(strExe)
        Else
            ShowMessage(strExe & " 不存在 ")
            Me.Close()
            Exit Sub
        End If

    End Sub

    Private Sub 打开ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开ToolStripMenuItem.Click
        ToolStripButton2_Click(sender, e)
    End Sub
End Class
